package com.xesj.serviceimpl;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xesj.dao.PermissionMapper;
import com.xesj.model.Permission;
import com.xesj.service.PermissionServiceI;
import com.xesj.service.RoleServiceI;
import com.xesj.util.PageData;

import net.sf.ehcache.pool.impl.FromLargestCacheOnDiskPoolEvictor;

@Service("permissionService")
@Transactional
public class PermissionServiceImpl implements PermissionServiceI {
	
	@Autowired
	private PermissionMapper permissionDao;
	@Resource
    private RoleServiceI roleService;

	@Override
	public int savePermission(Permission permission) {
		// TODO Auto-generated method stub
		return permissionDao.savePermission(permission);
	}

	@Override
	public PageData findPermissionById(PageData searchpd) {
		// TODO Auto-generated method stub
		return permissionDao.findPermissionById(searchpd);
	}

	@Override
	public List<PageData> allPermissionList(PageData pd) {
		List<PageData> list = permissionDao.findPermissionListByParentId(pd);
		if (list.size()!=0) {
			for (PageData paged : list) {
				PageData pds = new PageData();
				String permissionId = (String) paged.get("permissionId");
				String parentId = (String) paged.get("parentId");
				pds.put("parentId", parentId);
				paged.put("id", permissionId);
				paged.put("pId", parentId);
				paged.put("name", (String) paged.get("permissionName"));
				paged.put("checked", false);
				paged.put("target", "treeFrame");
				paged.put("url", "permissionController/findPermissionListByParentId.do?parentid=" + permissionId);
				paged.put("nodes", allPermissionList(pds));
			}
		}else {
			list =new ArrayList<PageData>();
		}
		return list;
	}

	@Override
	public List<Permission> getPermissionsInfoListByName(PageData pd) {
			
		return permissionDao.getPermissionsInfoListByName(pd);
	}

	@Override
	public List<Permission> getTreePermissionsListByName(PageData pd) {
		List<Permission> permissionsInfoList = permissionDao.getPermissionsInfoListByName(pd);
		String continues = pd.getString("continue");
		if (permissionsInfoList.size()!=0&&continues!=null&&continues.equals("true")) {
			for (Permission permission : permissionsInfoList) {
				PageData pdnew = new PageData();
				pdnew.put("loginName", pd.getString("loginName"));
				pdnew.put("parentId", permission.getPermissionId());
				permission.setChildrenPermission(getTreePermissionsListByName(pdnew));
			}
		}
		return permissionsInfoList;
	}

	

}
